package com.addzero.addzero_common

import com.addzero.common.consts.sql
import com.addzero.common.kt_util.toBitmask
import com.addzero.web.infra.jimmer.setJimmerEntity
import com.addzero.web.modules.second_brain.dotfiles.*
import org.babyfish.jimmer.sql.kt.KSqlClient
import org.babyfish.jimmer.sql.kt.ast.expression.KExpression
import org.babyfish.jimmer.sql.kt.ast.expression.KNonNullExpression
import org.babyfish.jimmer.sql.kt.ast.expression.sql
import org.babyfish.jimmer.sql.kt.ast.query.KConfigurableRootQuery
import org.babyfish.jimmer.sql.kt.ast.query.KMutableRootQuery
import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.TestConstructor



@SpringBootTest
@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
class 测试枚举插入(
    val sql: KSqlClient,
) {


    @Test
    fun 测试枚举插入(): Unit {
        val bizDotfiles = BizDotfiles {
            osType = listOf(EnumOsType.MAC, EnumOsType.LINUX) //保存MAC和LINUX
            osStructure = Enumplatforms.ARM64
            defType = EnumDefType.FUNCTION
            name = "visual-studio-code"
            value = "oxxxxxxxxxxxx"
            describtion = "vscode编辑器"
            status = EnumStatus.QIYONG
            fileUrl = null
            location = "/Applications/Visual Studio Code.app"
        }
//        val save = sql.save(bizDotfiles)
        println()
    }



    @Test
    fun `测试ListEnum查询`() {
        val execute = sql.createQuery(BizDotfiles::class) {
            val value = listOf(EnumOsType.WIN, EnumOsType.MAC) //期望能查出来
//            val value1 = listOf(EnumOsType.WIN)//期望查不出,因为没存WIN
            val predicates = table.osType `enumValueIn?` value
            where(predicates)
            select(table)
        }.execute()
        println()
    }


    infix fun <E : Enum<E>> KExpression<List<E>>.`enumValueIn?`(enums: Collection<E>): KNonNullExpression<Boolean>? {
        if (enums.isEmpty()) {
            return null
        }
        val bit = enums.toBitmask()
        return sql(Boolean::class, "(%e & %v) <> 0") {
            expression(this@`enumValueIn?`)
            value(bit)
        }
    }


}
